library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(tidyverse)
## Warning: package 'readr' was built under R version 4.3.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats 1.0.0 ✔ readr 2.1.5
## ✔ ggplot2 3.4.4 ✔ stringr 1.5.1
## ✔ lubridate 1.9.3 ✔ tibble 3.2.1
## ✔ purrr 1.0.2 ✔ tidyr 1.3.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(plotly)
## Warning: package 'plotly' was built under R version 4.3.3
##
## Attaching package: 'plotly'
##
## The following object is masked from 'package:ggplot2':
##
## last_plot
##
## The following object is masked from 'package:stats':
##
## filter
##
## The following object is masked from 'package:graphics':
##
## layout
library(reshape2)
## Warning: package 'reshape2' was built under R version 4.3.3
##
## Attaching package: 'reshape2'
##
## The following object is masked from 'package:tidyr':
##
## smiths
Co2_emission = read.csv('C:/Users/rushi/OneDrive/Desktop/GMU/Spring 24/STAT 515/Project/CO2_emission.csv')
Co2_2021 = Co2_emission %>%
filter(Year == 2021) %>%
replace(is.na(.), 0)
top_emitters <- Co2_2021 %>%
arrange(desc(Total)) %>%
head(20)
fig1 = plot_ly(top_emitters, x = ~Total, y = ~reorder(Country, Total), type = 'bar', orientation = 'h') %>%
layout(title = "Top 20 Countries by Total Emissions in 2021",
xaxis = list(title = "Total Emissions (MtCO2)"),
yaxis = list(title = "Country"))
fig1
data_2021 <- Co2_emission %>%
filter(Year == 2021) %>%
replace(is.na(.), 0) %>%
arrange(desc(Total)) %>%
head(7)
data_melted <- melt(data_2021, id.vars = c("Country", "ISO.3166.1.alpha.3", "Year", "Total"),
variable.name = "Fuel_Type", value.name = "Emissions")
data_melted <- data_melted %>%
group_by(Country) %>%
mutate(Percentage = Emissions / sum(Emissions) * 100)
percentage_plot <- plot_ly(data = data_melted, x = ~Country, y = ~Percentage, type = 'bar', color = ~Fuel_Type,
text = ~paste0(round(Percentage, 1), "%"), textposition = 'auto',
hoverinfo = 'text+x+y') %>%
layout(title = "Percentage of Emissions by Fuel Type in Top 7 Countries (2021)",
xaxis = list(title = "Country"),
yaxis = list(title = "Percentage (%)"),
barmode = 'stack')
percentage_plot
p1 <- plot_ly(data = data_melted, x = ~Country, y = ~Emissions, color = ~Fuel_Type, type = 'bar',
marker = list(line = list(width = 1))) %>%
layout(title = "Emissions by Fuel Type in Top 7 Countries (2021)",
xaxis = list(title = "Country"),
yaxis = list(title = "Emissions (MtCO2)"),
barmode = 'group',
showlegend = TRUE
,
transforms = list(
list(
type = 'groupby',
groups = ~Fuel_Type
)
)
)
p1
## Warning: 'layout' objects don't have these attributes: 'transforms'
## Valid attributes include:
## '_deprecated', 'activeshape', 'annotations', 'autosize', 'autotypenumbers', 'calendar', 'clickmode', 'coloraxis', 'colorscale', 'colorway', 'computed', 'datarevision', 'dragmode', 'editrevision', 'editType', 'font', 'geo', 'grid', 'height', 'hidesources', 'hoverdistance', 'hoverlabel', 'hovermode', 'images', 'legend', 'mapbox', 'margin', 'meta', 'metasrc', 'modebar', 'newshape', 'paper_bgcolor', 'plot_bgcolor', 'polar', 'scene', 'selectdirection', 'selectionrevision', 'separators', 'shapes', 'showlegend', 'sliders', 'smith', 'spikedistance', 'template', 'ternary', 'title', 'transition', 'uirevision', 'uniformtext', 'updatemenus', 'width', 'xaxis', 'yaxis', 'barmode', 'bargap', 'mapType'